//-----------------------------------------------------------------------
// <copyright file="a_ShoppingCartDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_ShoppingCartDAOBase
    {
        #region Common methods
        public virtual a_ShoppingCart Createa_ShoppingCartFromReader(IDataReader reader)
        {
            a_ShoppingCart item = new a_ShoppingCart();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) item.ID = (int)reader["ID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CookieID"))) item.CookieID = (string)reader["CookieID"];
                if (!reader.IsDBNull(reader.GetOrdinal("ProdID"))) item.ProdID = (int)reader["ProdID"];
                if (!reader.IsDBNull(reader.GetOrdinal("OptionCode"))) item.OptionCode = (string)reader["OptionCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("Quantity"))) item.Quantity = (int)reader["Quantity"];
                if (!reader.IsDBNull(reader.GetOrdinal("Price"))) item.Price = (decimal)reader["Price"];
                if (!reader.IsDBNull(reader.GetOrdinal("Amount"))) item.Amount = (decimal)reader["Amount"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomainID"))) item.DomainID = (int)reader["DomainID"];
                if (!reader.IsDBNull(reader.GetOrdinal("TimeIn"))) item.TimeIn = (DateTime)reader["TimeIn"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsCoupon"))) item.IsCoupon = (bool)reader["IsCoupon"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsinvalidCoupon"))) item.IsinvalidCoupon = (bool)reader["IsinvalidCoupon"];
                if (!reader.IsDBNull(reader.GetOrdinal("Discount"))) item.Discount = (decimal)reader["Discount"];
                if (!reader.IsDBNull(reader.GetOrdinal("AdjAmount"))) item.AdjAmount = (decimal)reader["AdjAmount"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsGetThat"))) item.IsGetThat = (byte)reader["IsGetThat"];
                if (!reader.IsDBNull(reader.GetOrdinal("NofNotexcess"))) item.NofNotexcess = (int)reader["NofNotexcess"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsSpecials"))) item.IsSpecials = (bool)reader["IsSpecials"];
                if (!reader.IsDBNull(reader.GetOrdinal("Isbuy"))) item.Isbuy = (bool)reader["Isbuy"];
                if (!reader.IsDBNull(reader.GetOrdinal("Iscombinable"))) item.Iscombinable = (bool)reader["Iscombinable"];
                if (!reader.IsDBNull(reader.GetOrdinal("CouponValueType"))) item.CouponValueType = (bool)reader["CouponValueType"];
                if (!reader.IsDBNull(reader.GetOrdinal("Typecoupon"))) item.Typecoupon = (int)reader["Typecoupon"];
                if (!reader.IsDBNull(reader.GetOrdinal("Discount2"))) item.Discount2 = (decimal)reader["Discount2"];
                if (!reader.IsDBNull(reader.GetOrdinal("AdjAmount2"))) item.AdjAmount2 = (decimal)reader["AdjAmount2"];
                if (!reader.IsDBNull(reader.GetOrdinal("Subdiscount"))) item.Subdiscount = (decimal)reader["Subdiscount"];
                if (!reader.IsDBNull(reader.GetOrdinal("CouponID"))) item.CouponID = (int)reader["CouponID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CouponValue"))) item.CouponValue = (int)reader["CouponValue"];
                if (!reader.IsDBNull(reader.GetOrdinal("RedemptionCode"))) item.RedemptionCode = (string)reader["RedemptionCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsCoupon2"))) item.IsCoupon2 = (bool)reader["IsCoupon2"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsinvalidCoupon2"))) item.IsinvalidCoupon2 = (bool)reader["IsinvalidCoupon2"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsGetThat2"))) item.IsGetThat2 = (byte)reader["IsGetThat2"];
                if (!reader.IsDBNull(reader.GetOrdinal("NofNotExcess2"))) item.NofNotExcess2 = (int)reader["NofNotExcess2"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsSpecials2"))) item.IsSpecials2 = (bool)reader["IsSpecials2"];
                if (!reader.IsDBNull(reader.GetOrdinal("Isbuy2"))) item.Isbuy2 = (bool)reader["Isbuy2"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_ShoppingCart methods
            
        public virtual void Createa_ShoppingCart(a_ShoppingCart a_ShoppingCart)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ShoppingCart_ins");
                
                database.AddInParameter(dbCommand, "@CookieID", DbType.AnsiString, a_ShoppingCart.CookieID);
                database.AddInParameter(dbCommand, "@ProdID", DbType.Int32, a_ShoppingCart.ProdID);
                database.AddInParameter(dbCommand, "@OptionCode", DbType.AnsiString, a_ShoppingCart.OptionCode);
                database.AddInParameter(dbCommand, "@Quantity", DbType.Int32, a_ShoppingCart.Quantity);
                database.AddInParameter(dbCommand, "@Price", DbType.Currency, a_ShoppingCart.Price);
                database.AddInParameter(dbCommand, "@Amount", DbType.Currency, a_ShoppingCart.Amount);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_ShoppingCart.DomainID);
                database.AddInParameter(dbCommand, "@TimeIn", DbType.DateTime, a_ShoppingCart.TimeIn);
                database.AddInParameter(dbCommand, "@IsCoupon", DbType.Boolean, a_ShoppingCart.IsCoupon);
                database.AddInParameter(dbCommand, "@IsinvalidCoupon", DbType.Boolean, a_ShoppingCart.IsinvalidCoupon);
                database.AddInParameter(dbCommand, "@Discount", DbType.Currency, a_ShoppingCart.Discount);
                database.AddInParameter(dbCommand, "@AdjAmount", DbType.Currency, a_ShoppingCart.AdjAmount);
                database.AddInParameter(dbCommand, "@IsGetThat", DbType.Byte, a_ShoppingCart.IsGetThat);
                database.AddInParameter(dbCommand, "@NofNotexcess", DbType.Int32, a_ShoppingCart.NofNotexcess);
                database.AddInParameter(dbCommand, "@IsSpecials", DbType.Boolean, a_ShoppingCart.IsSpecials);
                database.AddInParameter(dbCommand, "@Isbuy", DbType.Boolean, a_ShoppingCart.Isbuy);
                database.AddInParameter(dbCommand, "@Iscombinable", DbType.Boolean, a_ShoppingCart.Iscombinable);
                database.AddInParameter(dbCommand, "@CouponValueType", DbType.Boolean, a_ShoppingCart.CouponValueType);
                database.AddInParameter(dbCommand, "@Typecoupon", DbType.Int32, a_ShoppingCart.Typecoupon);
                database.AddInParameter(dbCommand, "@Discount2", DbType.Currency, a_ShoppingCart.Discount2);
                database.AddInParameter(dbCommand, "@AdjAmount2", DbType.Currency, a_ShoppingCart.AdjAmount2);
                database.AddInParameter(dbCommand, "@Subdiscount", DbType.Currency, a_ShoppingCart.Subdiscount);
                database.AddInParameter(dbCommand, "@CouponID", DbType.Int32, a_ShoppingCart.CouponID);
                database.AddInParameter(dbCommand, "@CouponValue", DbType.Int32, a_ShoppingCart.CouponValue);
                database.AddInParameter(dbCommand, "@RedemptionCode", DbType.AnsiString, a_ShoppingCart.RedemptionCode);
                database.AddInParameter(dbCommand, "@IsCoupon2", DbType.Boolean, a_ShoppingCart.IsCoupon2);
                database.AddInParameter(dbCommand, "@IsinvalidCoupon2", DbType.Boolean, a_ShoppingCart.IsinvalidCoupon2);
                database.AddInParameter(dbCommand, "@IsGetThat2", DbType.Byte, a_ShoppingCart.IsGetThat2);
                database.AddInParameter(dbCommand, "@NofNotExcess2", DbType.Int32, a_ShoppingCart.NofNotExcess2);
                database.AddInParameter(dbCommand, "@IsSpecials2", DbType.Boolean, a_ShoppingCart.IsSpecials2);
                database.AddInParameter(dbCommand, "@Isbuy2", DbType.Boolean, a_ShoppingCart.Isbuy2);
                database.AddOutParameter(dbCommand, "@ID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_ShoppingCart.ID = (int)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_ShoppingCart methods
        
        public virtual void Updatea_ShoppingCart(a_ShoppingCart a_ShoppingCart)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ShoppingCart_upd");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, a_ShoppingCart.ID);
                database.AddInParameter(dbCommand, "@CookieID", DbType.AnsiString, a_ShoppingCart.CookieID);
                database.AddInParameter(dbCommand, "@ProdID", DbType.Int32, a_ShoppingCart.ProdID);
                database.AddInParameter(dbCommand, "@OptionCode", DbType.AnsiString, a_ShoppingCart.OptionCode);
                database.AddInParameter(dbCommand, "@Quantity", DbType.Int32, a_ShoppingCart.Quantity);
                database.AddInParameter(dbCommand, "@Price", DbType.Currency, a_ShoppingCart.Price);
                database.AddInParameter(dbCommand, "@Amount", DbType.Currency, a_ShoppingCart.Amount);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_ShoppingCart.DomainID);
                database.AddInParameter(dbCommand, "@TimeIn", DbType.DateTime, a_ShoppingCart.TimeIn);
                database.AddInParameter(dbCommand, "@IsCoupon", DbType.Boolean, a_ShoppingCart.IsCoupon);
                database.AddInParameter(dbCommand, "@IsinvalidCoupon", DbType.Boolean, a_ShoppingCart.IsinvalidCoupon);
                database.AddInParameter(dbCommand, "@Discount", DbType.Currency, a_ShoppingCart.Discount);
                database.AddInParameter(dbCommand, "@AdjAmount", DbType.Currency, a_ShoppingCart.AdjAmount);
                database.AddInParameter(dbCommand, "@IsGetThat", DbType.Byte, a_ShoppingCart.IsGetThat);
                database.AddInParameter(dbCommand, "@NofNotexcess", DbType.Int32, a_ShoppingCart.NofNotexcess);
                database.AddInParameter(dbCommand, "@IsSpecials", DbType.Boolean, a_ShoppingCart.IsSpecials);
                database.AddInParameter(dbCommand, "@Isbuy", DbType.Boolean, a_ShoppingCart.Isbuy);
                database.AddInParameter(dbCommand, "@Iscombinable", DbType.Boolean, a_ShoppingCart.Iscombinable);
                database.AddInParameter(dbCommand, "@CouponValueType", DbType.Boolean, a_ShoppingCart.CouponValueType);
                database.AddInParameter(dbCommand, "@Typecoupon", DbType.Int32, a_ShoppingCart.Typecoupon);
                database.AddInParameter(dbCommand, "@Discount2", DbType.Currency, a_ShoppingCart.Discount2);
                database.AddInParameter(dbCommand, "@AdjAmount2", DbType.Currency, a_ShoppingCart.AdjAmount2);
                database.AddInParameter(dbCommand, "@Subdiscount", DbType.Currency, a_ShoppingCart.Subdiscount);
                database.AddInParameter(dbCommand, "@CouponID", DbType.Int32, a_ShoppingCart.CouponID);
                database.AddInParameter(dbCommand, "@CouponValue", DbType.Int32, a_ShoppingCart.CouponValue);
                database.AddInParameter(dbCommand, "@RedemptionCode", DbType.AnsiString, a_ShoppingCart.RedemptionCode);
                database.AddInParameter(dbCommand, "@IsCoupon2", DbType.Boolean, a_ShoppingCart.IsCoupon2);
                database.AddInParameter(dbCommand, "@IsinvalidCoupon2", DbType.Boolean, a_ShoppingCart.IsinvalidCoupon2);
                database.AddInParameter(dbCommand, "@IsGetThat2", DbType.Byte, a_ShoppingCart.IsGetThat2);
                database.AddInParameter(dbCommand, "@NofNotExcess2", DbType.Int32, a_ShoppingCart.NofNotExcess2);
                database.AddInParameter(dbCommand, "@IsSpecials2", DbType.Boolean, a_ShoppingCart.IsSpecials2);
                database.AddInParameter(dbCommand, "@Isbuy2", DbType.Boolean, a_ShoppingCart.Isbuy2);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_ShoppingCart methods
        public virtual void Deletea_ShoppingCart(int id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ShoppingCart_del");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_ShoppingCart methods
        
        public virtual a_ShoppingCart Geta_ShoppingCart(int id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ShoppingCart_sel");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                a_ShoppingCart a_ShoppingCart =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_ShoppingCart = Createa_ShoppingCartFromReader(reader);
                        reader.Close();
                    }
                }
                return a_ShoppingCart;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_ShoppingCartList methods
        public virtual a_ShoppingCartCollection Geta_ShoppingCartList(a_ShoppingCartColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ShoppingCart_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_ShoppingCartCollection a_ShoppingCartCollection = new a_ShoppingCartCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_ShoppingCart a_ShoppingCart = Createa_ShoppingCartFromReader(reader);
                        a_ShoppingCartCollection.Add(a_ShoppingCart);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_ShoppingCartCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_ShoppingCartCollection Geta_ShoppingCartList(a_ShoppingCartColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_ShoppingCartList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}